package com.stkouyu.xiansheng.net;


import android.os.Handler;
import android.os.Looper;


public class UIThreadUtil {
    private static Handler sHandler = new Handler(Looper.getMainLooper());

    public static void ensureRunOnMainThread(OnMainAction action) {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            sHandler.post(() -> {
                try {
                    action.action();
                } catch (Exception ignored) {
                    //empty
                }
            });
        } else {
            action.action();
        }

    }

    public static void ensureRunOnMainThreadDelay(OnMainAction action, long delay) {
        sHandler.postDelayed(action::action, delay);
    }

//    public static void runOnWorkThread(OnMainAction mainAction) {
//        Observable.just(1).observeOn(Schedulers.computation()).subscribe(integer -> {
//            if (mainAction != null) {
//                mainAction.action();
//            }
//        }, throwable -> {
//            //empty
//        });
//    }

    public interface OnMainAction {
        void action();
    }
}
